#include "UIButton.h"
#include "Scene.h"

using namespace GalliumEngine;

UIButton::UIButton(void)
{
}

UIButton::UIButton(std::string _id) : UIControl(_id)
{
}


UIButton::~UIButton(void)
{
}

void UIButton::initialize(std::string _upTexture,
	std::string _clickTexture,
	std::string _hoverTexture,
	Vector2 _position,
	Vector2 _size,
	int _depth,
	std::string shaderId,
	std::string _vertexShader,
	std::string _fragmentShader)
{
	UIControl::initialize();

	upTexture = ResourceManager::getInstance()->loadTexture(_upTexture);
	clickTexture = ResourceManager::getInstance()->loadTexture(_clickTexture);
	hoverTexture = ResourceManager::getInstance()->loadTexture(_hoverTexture);

	this->setPosition(_position);
	this->setSize(_size);
	
	this->setDepth(_depth);

	isHover = false;
	isClick = false;

	sticksDown = true;
	setVisible(true);

	isEnabled = true;

	setColour(Colour(1,1,1,1));

	this->addUv(Vector2(1,1)); 
	this->addVertex(Vector3((getSize()[0]),(getSize()[1]),0));

	this->addUv(Vector2(0,1)); 
	this->addVertex(Vector3(0,(getSize()[1]),0));

	this->addUv(Vector2(1,0)); 
	this->addVertex(Vector3((getSize()[0]),0,0));

	this->addUv(Vector2(0,0)); 
	this->addVertex(Vector3(0,0,0));

	this->addShaderParam(BindParams("modelVertex",BINDTYPE_ATTRIBUTE));
	this->addShaderParam(BindParams("uv",BINDTYPE_ATTRIBUTE));
	this->addShaderParam(BindParams("diffuseTextureSampler",BINDTYPE_UNIFORM));
	this->addShaderParam(BindParams("colour",BINDTYPE_UNIFORM));
	this->addShaderParam(BindParams("mvp",BINDTYPE_UNIFORM));

	this->setShader(ResourceManager::getInstance()->loadShader(shaderId,
		_vertexShader,
		"",
		_fragmentShader,
		this->getShaderParams()));

	this->setFragmentShader(_fragmentShader);
	this->setVertexShader(_vertexShader);

	bind();
}

void UIButton::deinitialize()
{
	
	//glDeleteVertexArrays(1, &vertexArrayID);

	UIControl::deinitialize();
}

void UIButton::setEnabled(bool _enabled)
{
	isEnabled = _enabled;
}

void UIButton::setSticksDown(bool _sticksDown)
{
	sticksDown = _sticksDown;
}

void UIButton::bind()
{	
	UIControl::bind();
}

void UIButton::update(float delta)
{	
	UIControl::update(delta);

	if(isEnabled)
	{
		if(getVisible())
		{
			Coordinate c = Mouse::getPosition();

			if(this->getPosition()[0] < c[0] &&
				this->getPosition()[1] < c[1] &&
				(this->getPosition()[0] + this->getSize()[0]) > c[0] &&
				(this->getPosition()[1] + this->getSize()[1]) > c[1])
			{
				isHover = true;
				OnEnter(EventData());
			}

			if(isHover)
			{
				if(this->getPosition()[0] > c[0] ||
				this->getPosition()[1] > c[1] ||
				(this->getPosition()[0] + this->getSize()[0]) < c[0] ||
				(this->getPosition()[1] + this->getSize()[1]) < c[1])
				{
					isHover = false;
					OnLeave(EventData());
				}
			}


			if(isHover)
			{
				if(Mouse::isButtonDown(MouseButtons::Left))
				{
					isClick = true;				
				}
			}

			if(isClick)
			{
				
				if(!Mouse::isButtonDown(MouseButtons::Left))
				{
					isClick = false;
					OnClick(EventData());
				}
				
			}
		}
	}
			
	setControlLocal(Matrix4::toMatrix4(this->getRotationZ(),Vector3(this->getPosition()[0],this->getPosition()[1],-this->getDepth()),getScale()));
}

void UIButton::draw()
{
	UIControl::draw();

	if(getVisible())
	{
		//Camera* cam = this->getScene()->getActiveOrthographicCamera();
		//setControlWorld(getControlLocal());
		//Matrix4 modelview = cam->getObjectToWorld().inverse() * getControlWorld();
		//Matrix4 mvp = cam->getProjectionMatrix()  * modelview;

		//this->getShader()->begin();

		//glUniformMatrix4fv((*this->getShader())["mvp"], 1, GL_FALSE, mvp.transpose().getArray());

		//GLuint texId = 0; 

		//if(isHover && isClick)
		//{
		//	//(*this->getShader())["diffuseTextureSampler"],0
		//	clickTexture->bindTexture();;
		//}
		//else if(isHover)
		//{
		//	//(*this->getShader())["diffuseTextureSampler"],0
		//	hoverTexture->bindTexture();;
		//}
		//else
		//{
		//	//(*this->getShader())["diffuseTextureSampler"],0
		//	upTexture->bindTexture();;
		//}

		//if(isEnabled)
		//{
		//	glUniform4fv((*this->getShader())["colour"],
		//		4,
		//		getColour().getArray());
		//}
		//else
		//{
		//	glUniform4f((*this->getShader())["colour"],
		//		0.5f,
		//		0.5f,
		//		0.5f,
		//		getColour().getA());
		//}

		//glEnableVertexAttribArray(0);
		//glBindBuffer(GL_ARRAY_BUFFER, this->getVertexbuffer());
		//glVertexAttribPointer(
		//	(*this->getShader())["modelVertex"],	// attribute
		//	3,							// size
		//	GL_FLOAT,					// type
		//	GL_FALSE,					// normalized?
		//	0,							// stride
		//	(void*)0					// array buffer offset
		//);

		//// 2nd attribute buffer : UVs
		//glEnableVertexAttribArray(1);
		//glBindBuffer(GL_ARRAY_BUFFER, this->getUvbuffer());
		//glVertexAttribPointer(
		//	(*this->getShader())["uv"],			// attribute
		//	2,							// size
		//	GL_FLOAT,					// type
		//	GL_FALSE,					// normalized?
		//	0,							// stride
		//	(void*)0					// array buffer offset
		//);

		////// Draw the triangle !
		//glDrawArrays(GL_TRIANGLE_STRIP, 0, this->getVertices().size());

		//glBindTexture(GL_TEXTURE_2D, 0);

		//glDisableVertexAttribArray(0);
		//glDisableVertexAttribArray(1);

		//this->getShader()->end();
	}
}
